package com.qujia.station.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qujia.common.constant.UserConstants;
import com.qujia.common.core.domain.entity.SysUser;
import com.qujia.common.core.domain.model.LoginUser;
import com.qujia.common.exception.CustomException;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.station.domain.BusinessAudit;
import com.qujia.station.domain.Enterprise;
import com.qujia.station.domain.Station;
import com.qujia.station.mapper.BusinessAuditMapper;
import com.qujia.station.service.BusinessAuditService;
import com.qujia.station.service.IEnterpriseService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (BusinessAudit)表服务实现类
 *
 * @author makejava
 * @since 2023-12-19 16:26:32
 */
@Service("businessAuditService")
public class BusinessAuditServiceImpl implements BusinessAuditService {
    @Resource
    private BusinessAuditMapper businessAuditMapper;
    @Resource
    private IEnterpriseService enterpriseService;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BusinessAudit queryById(Long id) {
        return this.businessAuditMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param businessAudit 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<BusinessAudit> queryByPage(BusinessAudit businessAudit, PageRequest pageRequest) {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysUser user = currentUser.getUser();
        if ("7".equals(user.getUserLevel())){
            List<Enterprise> enterpriseList =
                    enterpriseService.list(new QueryWrapper<Enterprise>().in("author_id", user.getUserId()));
            if (enterpriseList != null && enterpriseList.size()>0) {
                List<Long> enterpriseIds = enterpriseList.stream().map(Enterprise::getEnterpriseId).collect(Collectors.toList());
                businessAudit.setEnterpriseIdListForAuthor(enterpriseIds);
            } else {
                throw new CustomException(UserConstants.NO_ENTERPRISE);
            }
        }
        long total = this.businessAuditMapper.count(businessAudit);
        return new PageImpl<>(this.businessAuditMapper.queryAllByLimit(businessAudit, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param businessAudit 实例对象
     * @return 实例对象
     */
    @Override
    public BusinessAudit insert(BusinessAudit businessAudit) {
        this.businessAuditMapper.insert(businessAudit);
        return businessAudit;
    }

    /**
     * 修改数据
     *
     * @param businessAudit 实例对象
     * @return 实例对象
     */
    @Override
    public BusinessAudit update(BusinessAudit businessAudit) {
        this.businessAuditMapper.update(businessAudit);
        return this.queryById(businessAudit.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.businessAuditMapper.deleteById(id) > 0;
    }

    @Override
    public int updateByIds(BusinessAudit businessAudit) {
        return this.businessAuditMapper.updateByIds(businessAudit);
    }
}
